package com.larissa.android.quiz;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.larissa.android.quiz.databinding.FragmentQuestionDetailBinding;

public class QuestionDetailFragment extends Fragment {
    private static final String TAG="QuestionDetailFragment";
    private FragmentQuestionDetailBinding binding;
    private QuestionViewModel viewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel=new ViewModelProvider(requireActivity()).get(QuestionViewModel.class);
        Log.d(TAG,"onCreate() called");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        binding=FragmentQuestionDetailBinding.inflate(inflater);
        Log.d(TAG,"onCreateView() called");
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d(TAG,"onViewCreated() called");
        showQuestion();
        SaveClick save=new SaveClick();
        MoveClick move=new MoveClick();

        binding.btnSave.setOnClickListener(save);
        binding.btnCancel.setOnClickListener(save);
        binding.btnPrev.setOnClickListener(move);
        binding.btnNext.setOnClickListener(move);
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        Log.d(TAG,"onViewStateRestored() called");
    }

    private void showQuestion(){
        Question question= viewModel.getQuestion();
        binding.txQuestion.setText(question.getQuestionId());
        int checkButton=question.isQuestionAnswer()?R.id.cbx_true:R.id.cbx_false;
        binding.chosenAnswer.check(checkButton);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG,"onDestroyView() called");
        binding=null;
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG,"onStart() called");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG,"onResume() called");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG,"onPause() called");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG,"onStop() called");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy() called");
    }

    private class SaveClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if(v.getId()==R.id.btn_save){
                boolean answer=binding.chosenAnswer.getCheckedRadioButtonId()==R.id.cbx_true;
                viewModel.saveQuestion(answer);
            }
            showQuestion();
        }
    }

    private class MoveClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if(v.getId()==R.id.btn_prev){
                if(viewModel.getPrevQuestion()){
                    showQuestion();
                }else
                    Toast.makeText(requireActivity(),"This is the first question",Toast.LENGTH_SHORT).show();
            }else if(v.getId()==R.id.btn_next){
                if(viewModel.getNextQuestion()){
                    showQuestion();
                }else
                    Toast.makeText(requireActivity(), "This is the last question", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
